<?php

	/** Sous fonctionnalité de listing de membres de l'association 
	 * @author Sebastian Mosser
	 */


class sfMemberList extends aSubFeature {


   public function __construct() {
      $this->_acl = new AccessControlList();
      $this->_acl->setGranted(3); $this->_acl->setGranted(7);
      $this->_acl->setGranted(2);
   }


	protected function run() {
			
		$listingType = PropRetriever::getProp($_POST,"type");

		switch($listingType) {
			case "male":
				return $this->listMaleCategory();
			case "female":
				return $this->listFemaleCategory();
			case "clubFunction":
				return $this->listClubFunction();
			case "cotisation":
				return $this->listCotisation();
			case "assembly":
				return $this->listAssembly();
			default:
				throw new Exception("Type de listing non supporté !");
		}

	}

	private function categoryHeader() {
		$result = <<<EOS
		<tr>
			<th> No. </th> <th> Licence </th> <th> Nom </th> 
			<th> Date de Naissance </th>
			<th> Adresse </th> <th> Tel. Fixe </th> <th> Tel. Portable </th>
		</tr>
EOS;
		return $result;
	}

	private function cotisationOkHeader() {
	  $result = <<<EOS
	    <tr>
	    <th> No. </th> <th> Licence </th> <th> Nom </th> 
	    <th> Date de Naissance </th>
	    <th> Adresse </th> <th> Informations </th>
	    </tr>
EOS;
	  return $result;
	}

	private function displayCotisationOk($i,$aMember) {
	  // Member info
	  $licence = $aMember->getLicenceNo();
	  $name = $aMember->getLastName() . " " . $aMember->getFirstName();
	  $adress = $aMember->getAddress();
	  $birth = $aMember->getBirthDate()->prettyPrint();
	  
	  //cotisation
	  $cotis = $aMember->getCotisation(Login::seasonChoosen(False));
	  $cotis = Cotisation::read($cotis["cotisation_type"]);
	  $cotisInfo = $cotis->displayLong();
	  $line = <<<EOS
	    <td>${i}</td> <td>${licence}</td> <td>${name}</td>
	    <td>${birth}</td><td>${adress}</td><td>${cotisInfo}</td>
EOS;
	  return $line;
	}

	private function listAssembly() {
		$aSeason = Season::read(Login::seasonChoosen(False));
					
		$title = 
			"<h1> Membres Votants pour la saison " .$aSeason->displayShort(). "</h1>";

		$date = PropRetriever::getProp($_POST,"date");
		if ($date == "")
			return "<div align='center'> <span class='error'> Date manquante </span> </div>";
			
			$date = Date::fromSQL($date);
			$title .= "<h2> Assemblée du ".$date->prettyPrint()."</h2>";
		
			$year = $date->getYear();
			
			
		$header =<<<EOS
<tr>
	<th> Licence </th> <th> Nom </th> <th> Prénom </th>
	<th> Adresse </th> <th> Code Postal </th> <th> Ville </th>
	<th> Date de Naissance </th> <th> Date d'entrée </th>
	<th> Fonction </th> <th> Cotisation ? </th>
<tr>
EOS;
	
		$members = Member::lookFor(array("lastName" => "%"));
		$content = "";
		while (($row = Database::assoc($members)) != False) {
			$aMember = Member::read($row["id"]);
			$functionId = $aMember->getFunction(Login::seasonChoosen(False));
			
			if ($functionId == -1 || $functionId == ClubFunction::$OUT)
				continue;
			
			// On check sur l'age ...
			$birthDate = $aMember->getBirthDate();
			$birthYear = $birthDate->getYear();
			if ($year - $birthYear < 18)
				continue; // Pas majeur, degage ...
			$licence = $aMember->getLicenceNo();
			$name = $aMember->getLastName();
			$firstname = $aMember->getFirstName();
			$adress =  $aMember->getAddress();
			$lcity = $aMember->getLivingCity();
			if ($lcity == -1) {
				$city = "<td> ?? </td> <td> ?? </td>";
			}
			else 
				$city = City::read($lcity)->displayInline();
			$birth = $birthDate->displaySquizzed();
			$entry = $aMember->getEntryDate()->displaySquizzed();
			
			// Test sur la date d'entree
			$entryDate = $aMember->getEntryDate();
			$entryTime = mktime(0,0,0,$entryDate->getMonth(),$entryDate->getDay(),$entryDate->getYear());
			$nowTime = time();
			if ( ($nowTime - $entryTime) < (60*60*24*31*6))
			  continue;
			$function = ClubFunction::read($functionId)->displayShort();
			$cotisation = $aMember->getCotisation(Login::seasonChoosen(False));
			$cotisation = ($cotisation == -1 ? "Impayée" : "Payée");
			
			$content .= <<<EOS
	<tr> 
		<td> $licence </td> <td> $name </td> <td> $firstname </td>
		<td> $adress </td> $city 
		<td> $birth </td> <td> $entry </td> <td> $function </td>
		<td> $cotisation </td>
	</tr>
EOS;
			
		}
		
		if ($content == "")
			$page = "<div align='center'> <span class='error'> Aucun membres </span> </div>";
		else {
			$page = <<<EOS
${title}
<div align="center">
<table class="resultset">
${header}
${content}
</table>
</div>
EOS;
		}
		return $page;
	
	}

	private function listCotisation() {
		$type = PropRetriever::getProp($_POST,"cotisation");
			
		$aSeason = Season::read(Login::seasonChoosen(False));
		
		if ($type == "KO")
			$title = "Cotisations Impayées en " . $aSeason->displayShort() . "</h1>";
		else
			$title = "Cotisations Payées en " . $aSeason->displayShort() . "</h1>";

		$page = "<h1> $title </h1>" ;
		
		$i = 1;
		$members = Member::lookFor(array("lastName" => "%"));
		$display = "";
		while (($row = Database::assoc($members)) != False) {
			$aMember = Member::read($row["id"]);
			$functionId = $aMember->getFunction(Login::seasonChoosen(False));
			if ($functionId == -1 || $functionId == ClubFunction::$OUT)
				continue;
			if ($type == "KO" && 
					    $aMember->getCotisation(Login::seasonChoosen(False)) == -1)
				$display .= "<tr> " .$aMember->displayInListing($i++) . "</tr>\n";
			if ($type == "OK" && 
					    $aMember->getCotisation(Login::seasonChoosen(False)) != -1)
			  //$display .= "<tr> " .$aMember->displayInListing($i++) . "</tr>\n";
			  $display .= "<tr> " .$this->displayCotisationOk($i++,$aMember) . "</tr>\n";
		}
		if ($i == 1)
			$display = "<p align=\"center\"> Aucun adhérent a lister ! </p>";
		else {
			$headers = "<div align=\"center\"> <table class=\"resultset\">";
			if ($type == "KO")
			  $headers .= $this->categoryHeader();
			else
			  $headers .= $this->cotisationOkHeader();
			$display = $headers .$display;
			$display .= "</table> </div>";
		}
		return $page . $display;
	}

	private function listMaleCategory() {
		$catId = PropRetriever::getProp($_POST,"category");
		$catId = ($catId == "" ? 0 : $catId);
		$aSeason = Season::read(Login::seasonChoosen(False));
		
		$page = "<h1> Listing ".Category::$maleCat[$catId] ;
		$page .= " / Saison " . $aSeason->displayShort() . "</h1>";
		
		$i = 1;
		$display = "";
		$members = Member::lookFor(array("lastName" => "%"));
		while (($row = Database::assoc($members)) != False) {
			$aMember = Member::read($row["id"]);
			
			$isValid = ($aMember->getGender() == Member::$MALE) &&
				($aMember->getFunction(Login::seasonChoosen(False)) == ClubFunction::$PLAYER) &&
				(Category::compute($aMember->getBirthDate(),Member::$MALE) == $catId);
			if ($isValid) {
				$display .= "<tr> " .$aMember->displayInListing($i++) . "</tr>\n";
			}
		}
		if ($i == 1)
			$display = "<p align=\"center\"> Aucun adhérent a lister ! </p>";
		else {
			$headers = "<div align=\"center\"> <table class=\"resultset\">";
			$headers .= $this->categoryHeader();
			$display = $headers .$display;
			$display .= "</table> </div>";
		}
		
		return $page . $display;
	}

	private function listFemaleCategory() {
		$catId = PropRetriever::getProp($_POST,"category");
		$catId = ($catId == "" ? 0 : $catId);
		$aSeason = Season::read(Login::seasonChoosen(False));
		
		$page = "<h1> Listing ".Category::$femaleCat[$catId] ;
		$page .= " / Saison " . $aSeason->displayShort() . "</h1>";
		
		$i = 1;
		$display = "";
		$members = Member::lookFor(array("lastName" => "%"));
		while (($row = Database::assoc($members)) != False) {
			$aMember = Member::read($row["id"]);
			
			$isValid = ($aMember->getGender() == Member::$FEMALE) &&
				($aMember->getFunction(Login::seasonChoosen(False)) == ClubFunction::$PLAYER) &&
				(Category::compute($aMember->getBirthDate(),Member::$FEMALE) == $catId);
			if ($isValid) {
				$display .= "<tr> " .$aMember->displayInListing($i++) . "</tr>\n";
			}
		}
		if ($i == 1)
			$display = "<p align=\"center\"> Aucun adhérent a lister ! </p>";
		else {
			$headers = "<div align=\"center\"> <table class=\"resultset\">";
			$headers .= $this->categoryHeader();
			$display = $headers .$display;
			$display .= "</table> </div>";
		}
		
		return $page . $display;
	}


	private function listClubFunction() {
		$functionId = PropRetriever::getProp($_POST,"function");

		$aSeason = Season::read(Login::seasonChoosen(False));
		$aFunction = ClubFunction::read($functionId);
		
		$page = "<h1> Listing ". $aFunction->displayShort() ;
		$page .= " / Saison " . $aSeason->displayShort() . "</h1>";
		
		$i = 1;
		$display = "";
		$members = Member::lookFor(array("lastName" => "%"));
		while (($row = Database::assoc($members)) != False) {
			$aMember = Member::read($row["id"]);
			
			$isValid = 
				$aMember->getFunction(Login::seasonChoosen(False)) == $functionId;
			
			if ($isValid) {
				$display .= "<tr> " .$aMember->displayInListing($i++) . "</tr>\n";
			}
		}
		if ($i == 1)
			$display = "<p align=\"center\"> Aucun adhérent a lister ! </p>";
		else {
			$headers = "<div align=\"center\"> <table class=\"resultset\">";
			$headers .= $this->categoryHeader();
			$display = $headers .$display;
			$display .= "</table> </div>";
		}
		
		return $page . $display;
	
	}


}

?>